package com.ketai.edu.controller.admin;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.lqfj.common.constants.ResultCodeEnum;
import com.lqfj.common.exception.KetaiException;
import com.lqfj.common.vo.R;
import com.ketai.edu.entity.Teacher;
import com.ketai.edu.query.TeacherQuery;
import com.ketai.edu.service.TeacherService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiModelProperty;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Api(tags = "讲师管理")
@RestController
@RequestMapping("/admin/edu/teacher")
public class TeacherAdminController {
    @Autowired
    private TeacherService teacherService;

    @ApiOperation(value = "所有讲师列表")
    @GetMapping
    public R list() {
        List<Teacher> list = teacherService.list(null);
        return R.ok().data("items", list);
    }

    @ApiOperation(value = "根据ID删除讲师")
    @DeleteMapping("{id}")
    public R removeById(@ApiParam(name = "id", value = "讲师ID", required = true)
                        @PathVariable Integer id) {
        teacherService.removeById(id);
        return R.ok();
    }


    //分页
    @ApiModelProperty(value = "分页讲师列表")
    @GetMapping("{page}/{limit}")
    public R pageQuery(
            @ApiParam(name = "page", value = "当前页码", required = true)
            @PathVariable Integer page,
            @ApiParam(name = "limit", value = "每页记录数", required = true)
            @PathVariable Integer limit,
            @ApiParam(name = "teacherQuery", value = "查询对象", required = false)
                    TeacherQuery teacherQuery) {
        if (page<=0||limit<=0){
            throw new KetaiException(ResultCodeEnum.PARAM_ERROR);
        }
        Page<Teacher> pageParm = new Page<>(page, limit);
        teacherService.pageQuery(pageParm, teacherQuery);
        List<Teacher> records = pageParm.getRecords();
        long total = pageParm.getTotal();
        return R.ok().data("total", total).data("rows", records);

    }

//
//    //分页
//    @ApiModelProperty(value = "分页讲师列表")
//    @GetMapping("{page}/{limit}")
//    public R pageList(
//            @ApiParam(name = "page", value = "当前页码", required = true)
//            @PathVariable Integer page,
//            @ApiParam(name = "limit", value = "每页记录数", required = true)
//            @PathVariable Integer limit) {
//        Page<Teacher> pageParm = new Page<>(page, limit);
//        teacherService.page(pageParm, null);
//        List<Teacher> records = pageParm.getRecords();
//        long total = pageParm.getTotal();
//        return R.ok().data("total", total).data("rows", records);
//
//    }

    //新增
    @ApiOperation(value = "新增讲师")
    @PostMapping
    public R save(
            @ApiParam(name = "teacher", value = "讲师对象", required = true)
            @RequestBody Teacher teacher) {
        teacherService.save(teacher);
        return R.ok();
    }

    //根据id查询
    @ApiOperation(value = "根据ID查询讲师")
    @GetMapping("{id}")
    public R getById(
            @ApiParam(name = "id", value = "讲师ID", required = true)
            @PathVariable String id) {
        Teacher teacher=teacherService.getById(id);
        return R.ok().data("item",teacher);
    }

    //根据id修改
    @ApiOperation(value = "根据ID修改讲师")
    @PutMapping("{id}")
    public R updateById(
            @ApiParam(name = "id",value = "讲师ID",required = true)
            @PathVariable Integer id,
            @ApiParam(name = "teacher",value = "讲师对象",required = true)
            @RequestBody Teacher teacher){
        teacher.setId(id);
        teacherService.updateById(teacher);
        return R.ok();
    }


}
